package m.jclecai.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.adapter.AllRedPkgAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.dialog.ConvertRedDialog;
import m.jclecai.com.fragment.CovertRedFragment;
import m.jclecai.com.fragment.RedPkgFragment;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.BaseBody;

/**
 * Created by ZD on 2017/6/27.
 */

public class RedPkgActivity extends BaseActivity {

    List<BaseFragment> pages;
    AllRedPkgAdapter adapter;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    String[] title = new String[]{"可用", "已用完", "过期","卡密兑换"};
    ConvertRedDialog convertRedDialog;

    @Override
    protected int getContentView() {
        return R.layout.activity_red;
    }

    @Override
    protected void resetWindow() {

    }

    @Override
    protected void initView() {
        super.initView();
        leftOptionText("");
        setTitle("我的红包");
        rightOptionText("赠送红包");
    }

    @OnClick(R.id.tv_right_option)
    void convert() {
     //  startActivity(new Intent(this,CovertRedActivity.class));
        startActivity(new Intent(this,RedPackSendFriendActivity.class));
//        convertRedDialog = new ConvertRedDialog(this);
//        convertRedDialog.setCallback(new Callback() {
//            @Override
//            public void onClick(String[] param) {
//                super.onClick(param);
//                getRedPacket(param);
//            }
//        });
//        convertRedDialog.show();
    }

    /**
     *
     */
    void getRedPacket(String[] param) {
        showDialog();
        Map<String, String> pair = new HashMap<>();
        pair.put("user", PreferenceUtil.getUserName(this));//Yes	string	用户名
        pair.put("password", PreferenceUtil.getPassword(this));//Yes	string	密码
        pair.put("name", param[0]);//Yes	卡号
        pair.put("newValue", param[1]);//Yes	卡密
        HttpManager.postAsyn(Constant_Url.USERREDCARD, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                BaseBody body = FastOk.get(response, BaseBody.class);
                if (body != null) {
                    DisPlay(body.getMessage());
                    if (body.getStatus() == 0 && convertRedDialog != null)
                        convertRedDialog.dismiss();
                } else {
                    DisPlay(getString(R.string.error_network));
                }
            }
        }, pair);

    }

    @Override
    protected void initData() {
        super.initData();
        pages = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            RedPkgFragment redPkgFragment = new RedPkgFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("flag", i);
            redPkgFragment.setArguments(bundle);
            pages.add(redPkgFragment);
        }
        pages.add(new CovertRedFragment());

        adapter = new AllRedPkgAdapter(getSupportFragmentManager(), pages, title);
        viewPager.setAdapter(adapter);

        viewPager.setOffscreenPageLimit(3);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout) {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }
        });
        tabLayout.setupWithViewPager(viewPager);
    }


}
